package com.lianzhihui.romeex.ui.base;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.just.agentweb.WebViewClient;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.inter.CustomAllCallback;
import com.lianzhihui.romeex.utils.LogUtil;
import com.lianzhihui.romeex.widget.keybord.SafetyKeyBoardDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;

/**
 * Description:
 * Author: RAMON
 * CreateDate: 3/12/21 6:50 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class WebViewActivity extends BaseActivity {

    @BindView(R.id.webview)
    WebView webview;
//    @BindView(R.id.refresher)
//    SwipeRefreshLayout refresher;

    String url;
    Intent intent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview_layout;
    }

    @Override
    protected void initView() {
        intent = getIntent();
        url = intent.getStringExtra("url");
        String title = intent.getStringExtra("title");
        boolean isHT = intent.getBooleanExtra("isHideTitle", false);
        if (isHT) {
            hideBar();
        }
        setTitle(title);

        WebSettings settings = webview.getSettings();
        settings.setUseWideViewPort(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setLoadWithOverviewMode(true);
        settings.setDomStorageEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setAllowContentAccess(true);
        settings.setAllowFileAccess(true);
        settings.setAllowFileAccessFromFileURLs(false);
        settings.setAllowUniversalAccessFromFileURLs(false);
        settings.setJavaScriptEnabled(true);
        webview.addJavascriptInterface(new JS2AndroidUtil(), "android");

        webview.setLayerType(View.LAYER_TYPE_NONE, null);//加上此方法解决webview无法确定高度导致崩溃问题
        webview.setWebViewClient(mWebViewClient);
        webview.loadUrl(url);
    }

//    @Override
//    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//        webview.loadUrl(url);
//    }

    class JS2AndroidUtil {

        @JavascriptInterface
        public void safePayPopWindow(String function) {

            Log.e("ramon", "充值点击");
            SafetyKeyBoardDialog dialog = new SafetyKeyBoardDialog(mActivity);
            dialog.setCallback(new CustomAllCallback() {
                @Override
                public void onCompare(Object o) {
                    dialog.dismiss();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            String resUrl = "javascript:" + function + "()";
                            Log.e("ramon", resUrl);
//                            webview.loadUrl(resUrl);

                            webview.evaluateJavascript(resUrl, new ValueCallback<String>() {
                                @Override
                                public void onReceiveValue(String value) {
                                    //此处为 js 返回的结果
                                    Log.e("ramon",value);
                                }
                            });

                        }
                    });
                }

                @Override
                public void onFailure(Object o) {
                }

                @Override
                public void dissmiss() {

                }
            });
            dialog.show();
        }

    }

    @Override
    protected void initData() {

    }

    private WebViewClient mWebViewClient = new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if (request.getUrl().toString().contains("finishPage")) {
                finish();
                return true;
            }
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.e("ramon","page finish");
            String title = view.getTitle(); //网页标题
            if (!TextUtils.isEmpty(title) && !title.contains("http")) {
                setTitle(title);
            }
//            refresher.setRefreshing(false);
        }
    };

}
